package cn.shuangliang.contract.web;

import cn.shuangliang.contract.model.Product;
import cn.shuangliang.contract.service.TypeService;
import cn.shuangliang.contract.util.StrUtil;
import cn.shuangliang.contract.model.*;
import cn.shuangliang.contract.vo.Result;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author oldliu
 * @since 1.0
 */
public class TypeController {

    private TypeService typeService=new TypeService();

    public String index(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String sidu=request.getParameter("sidu");
        String style=request.getParameter("style");
        request.setAttribute("data",typeService.getTop(sidu,style));

        return "/WEB-INF/views/types.jsp";
    }
    public Result add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String sidu=request.getParameter("sidu");
        String style=request.getParameter("style");
        Long parent=StrUtil.toLong(request.getParameter("parent"));
        //Long id=StrUtil.toLong(request.getParameter("id"));
        String desc=request.getParameter("desc");
        Type type=new Type(sidu,style,desc,parent);
        int repeat= typeService.add(type);
        return  repeat==0? Result.error(String.format("%s-%s重复了",sidu,style)):Result.success(type);
    }
}
